import { Component, Vue } from 'vue-property-decorator'
import { PAGE, SEARCH,NOTE } from '@/store/modules/crud/keys'
import { AppState } from '@/store/modules/app/typings'
import { CrudState } from '@/store/modules/crud/typings'
import cryptoJs from 'crypto-js';

@Component
export default class MiXinModal extends Vue {
    get _name(){ 
        return NOTE; 
    }
    get appState() { 
        return this.$store.state.app as AppState; 
    }
    get state() { 
        return this.$store.state.crud[this._name] as CrudState; 
    }
    get page() { 
        return this.state.page; 
    }
    get info(){ 
        return this.state.info; 
    }
    get appd() {
        return this.state.appd;
    }

    //随机生成指定数量的32进制key
    generatekey(): string {
        const num = 32;
        const library = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        let key = "";
        for (let i = 0; i < num; i++) {
            const randomPoz = Math.floor(Math.random() * library.length);
            key += library.substring(randomPoz, randomPoz + 1);
        }
        return key;
    }
    //DES加密
    encryptDes(message: string, key: string): string {
        const keyHex = cryptoJs.enc.Utf8.parse(key)
        const option = { mode: cryptoJs.mode.ECB, padding: cryptoJs.pad.Pkcs7 }
        const encrypted = cryptoJs.DES.encrypt(message, keyHex, option).toString()
        return encrypted
    }
    //DES解密
    decryptDes(message: string, key: string): string {
        const keyHex = cryptoJs.enc.Utf8.parse(key)
        const decrypted = cryptoJs.DES.decrypt(
            {
                ciphertext: cryptoJs.enc.Hex.parse(message)
            },
            keyHex,
            {
                mode: cryptoJs.mode.ECB,
                padding: cryptoJs.pad.Pkcs7
            }
        )
        return decrypted.toString(cryptoJs.enc.Utf8)
    }
    generateCheckBit(str: string){
        let sum = 0;
        for(let i=0; i<str.length;i++ ){
            let k = str.charCodeAt(i);
            sum = sum + k;
        }
        return sum;
    }
}